package org.csu.secondhand.api.controller.fronted;

import org.csu.secondhand.api.entity.Account;
import org.csu.secondhand.api.entity.Comments;
import org.csu.secondhand.api.service.AccountService;
import org.csu.secondhand.api.service.CommentsService;
import org.csu.secondhand.api.service.PostingService;
import org.csu.secondhand.util.TimeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/comments")
public class CommentsController {
    @Autowired
    CommentsService commentsService;
    @Autowired
    PostingService postingService;
    @Autowired
    AccountService accountService;

    @PostMapping("insertComments")
    @ResponseBody
    public Map<Object, Object> insertComments(String username,
                                              long posting,
                                              String content) {
        Map<Object, Object> map = new HashMap<>();
        Comments comments = commentsService.insertComments(
                new Comments(0, username, posting, content, TimeUtil.getTime()));
        postingService.commentsPlus(posting);
        map.put("code", 0);
        map.put("msg", "SUCCESS");
        map.put("data", comments);
        return map;
    }

    @PostMapping("deleteComments")
    @ResponseBody
    public Map<Object, Object> deleteComments(long id) {
        Map<Object, Object> map = new HashMap<>();
        postingService.commentsMinus(commentsService.getCommentsById(id).getPosting());
        Comments comments = commentsService.deleteComments(id);
        map.put("code", 0);
        map.put("msg", "SUCCESS");
        map.put("data", comments);
        return map;
    }

    @GetMapping("getCommentsByPosting")
    @ResponseBody
    public Map<Object, Object> getCommentsByPosting(long posting) {
        Map<Object, Object> map = new HashMap<>();
        List<Comments> commentsList = commentsService.getCommentsByPosting(posting);
        map.put("code", 0);
        map.put("msg", "SUCCESS");
        map.put("data", commentsList);
        return map;
    }

    @GetMapping("getCommentsByUser")
    @ResponseBody
    public Map<Object, Object> getCommentsByUser(String user) {
        Map<Object, Object> map = new HashMap<>();
        List<Comments> commentsList = commentsService.getCommentsByUser(user);
        map.put("code", 0);
        map.put("msg", "SUCCESS");
        map.put("data", commentsList);
        return map;
    }

    @GetMapping("getCommentsUserHeadImg")
    @ResponseBody
    public Map<Object, Object> getCommentsUserHeadImg(long posting) {
        Map<Object, Object> map = new HashMap<>();
        List<Comments> commentsList = commentsService.getCommentsByPosting(posting);
        List<String> imgList = new ArrayList<>();
        for(int i=0;i<commentsList.size();i++) {
            Account account = accountService.getAccount(commentsList.get(i).getUsername());
            imgList.add(account.getHeadimg());
        }
        map.put("code", 0);
        map.put("msg", "SUCCESS");
        map.put("data", imgList);
        return map;
    }

    @GetMapping("getCommentsUserName")
    @ResponseBody
    public Map<Object, Object> getCommentsUserName(long posting) {
        Map<Object, Object> map = new HashMap<>();
        List<Comments> commentsList = commentsService.getCommentsByPosting(posting);
        List<String> nameList = new ArrayList<>();
        for(int i=0;i<commentsList.size();i++) {
            Account account = accountService.getAccount(commentsList.get(i).getUsername());
            nameList.add(account.getUsername());
        }
        map.put("code", 0);
        map.put("msg", "SUCCESS");
        map.put("data", nameList);
        return map;
    }
}
